//
//  IMMessageListViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/5/19.
//

#import "IMMessageListViewController.h"
#import "NotificaView.h"
#import "IMListModel.h"
#import "IMListTableViewCell.h"
#import "IMMessageViewController.h"
#import "MessageViewController.h"
@interface IMMessageListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) NotificaView * topView;
@end

@implementation IMMessageListViewController
-(void)SQ_isShow{
    [self.topView SQ_show];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self.topView SQ_show];
    [self SQ_Header];
}
- (void)dealloc{
    DTLog(@"聊天列表页面释放");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"消息");
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_isShow) name:@"MessageViewController" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_Header) name:@"IMListModel" object:nil];
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"xiaoxi") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        MessageViewController* vc = [[MessageViewController alloc]init];
        [weakSelf SQ_pushViewControllerFull:vc];
    }];
    self.zx_navBar.zx_rightBtn.zx_fixImageSize = CGSizeMake(26, 26);
  
}
- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"listbg");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
        make.height.mas_equalTo(0);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.topView.mas_bottom);
            make.bottom.equalTo(self.view.mas_bottom);
    }];
}

- (void)SQ_Header{
    [self.jsonArray removeAllObjects];
    [self.jsonArray addObjectsFromArray:[SQFMDBManager SQ_IMAcquire]];
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IMListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IMListTableViewCell"];
    if (!cell) {
        cell = [[IMListTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"IMListTableViewCell"];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return   80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    IMMessageViewController * vc = [[IMMessageViewController alloc]init];
    vc.model = [self.jsonArray objectAtIndex:indexPath.item];
    [self SQ_pushViewControllerFull:vc];
}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf);
    UIContextualAction * delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:SQNSLocalString(@"删除") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        completionHandler(YES);
        [weakSelf SQ_deleted:indexPath];
    }];
    delete.backgroundColor = UIColor.redColor;
    UISwipeActionsConfiguration * config = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    return config;
}
-(void)SQ_deleted:(NSIndexPath*)indexPath{
  IMListModel *  model = [self.jsonArray objectAtIndex:indexPath.item];
   BOOL isok = [SQFMDBManager SQ_deletet:model.roomId];
    if (isok) {
        [self.jsonArray removeObject:model];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


- (NotificaView *)topView{
    if (!_topView) {
        _topView = [[NotificaView alloc]initWithFrame:CGRectZero];
    }
    return _topView;
}

@end
